/**
 * @author : zzr
 * @description :用双链表为底层实现队列Queue
 * @date :
 */
public class MyQueue {
    static class ListNode { //底层为双向链表
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) { //初始化
            this.val = val;
        }
    }
    private ListNode front; //队头
    private ListNode rear;  //队尾
    private int usedSize;

    public void offer(int x) {
        //采用头插法
        ListNode node = new ListNode(x);
        if (front == null) {
            //如果队列为空
            front = rear = node;
        } else {
            //队列不为空
            node.next = front;
            front.prev = node;
            front = node;
        }
        usedSize++;
    }

    public int poll() {
        if (front == null) {
            //如果链表为空
            return -1;
        }
        int ret = rear.val; //将尾节点的值记录下来

        if (front == rear) {
            front = null;
            rear = null;
            usedSize--;
            return ret;
        }

        rear = rear.prev;
        rear.next = null;
        usedSize--;
        return ret;
    }

    public int peek() {
        if (null == front) {
            return -1;
        }
        return rear.val;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public boolean isEmpty() {
        return null == front;
    }
}
